import { isString, isEmpty } from "@pureadmin/utils";
import { useMultiTagsStoreHook } from "@/store/modules/multiTags";
import {
  useRouter,
  useRoute,
  type LocationQueryRaw,
  type RouteParamsRaw
} from "vue-router";

export function useDetail() {
  const route = useRoute();
  const router = useRouter();
  const getParameter = isEmpty(route.params) ? route.query : route.params;

  function toDetail(
    parameter: LocationQueryRaw | RouteParamsRaw,
    model:
      | "addPjctInvoice"
      | "viewPjctInvoice"
      | "editPjctInvoice"
      | "invoPjctInvoice"
      | "reInvoPjctInvoice"
      | "exaimePjctInvoice"
      | "viewSales"
      | "viewSettlement"
      | "viewProjectOrder"
      | "viewAcceptanceForm"
  ) {
    Object.keys(parameter).forEach(param => {
      if (!isString(parameter[param])) {
        parameter[param] = parameter[param].toString();
      }
    });
    // 新建项目发票
    if (model === "addPjctInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/projectInvoiceDetail",
        name: "projectInvoiceDetail",
        query: parameter,
        meta: {
          title: `【新建】项目发票`
        }
      });
      router.push({ name: "projectInvoiceDetail", query: parameter });
    }
    // 查看发票详情
    if (model === "viewPjctInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/projectInvoiceDetail",
        name: "projectInvoiceDetail",
        query: parameter,
        meta: {
          title: `【查看】项目发票详情`
        }
      });
      router.push({ name: "projectInvoiceDetail", query: parameter });
    }
    // 编辑发票详情
    if (model === "editPjctInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/projectInvoiceDetail",
        name: "projectInvoiceDetail",
        query: parameter,
        meta: {
          title: `【编辑】项目发票详情`
        }
      });
      router.push({ name: "projectInvoiceDetail", query: parameter });
    }
    // 开票
    if (model === "invoPjctInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/projectInvoiceDetail",
        name: "projectInvoiceDetail",
        query: parameter,
        meta: {
          title: `【开票】项目发票开票`
        }
      });
      router.push({ name: "projectInvoiceDetail", query: parameter });
    }
    // 重新生成
    if (model === "reInvoPjctInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/projectInvoiceDetail",
        name: "projectInvoiceDetail",
        query: parameter,
        meta: {
          title: `【开票】项目发票重新生成`
        }
      });
      router.push({ name: "projectInvoiceDetail", query: parameter });
    }
    // 云采确认
    if (model === "exaimePjctInvoice") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_InvoiceManage/ERP_Projectinvoice/components/projectInvoiceDetail",
        name: "projectInvoiceDetail",
        query: parameter,
        meta: {
          title: `【核对】发票云采确认`
        }
      });
      router.push({ name: "projectInvoiceDetail", query: parameter });
    }
    // 查看销售合同详情
    else if (model === "viewSales") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_sales/components/salesContractDetail",
        name: "salesContractDetail",
        query: parameter,
        meta: {
          title: `【查看】${parameter.title}`
        }
      });
      router.push({ name: "salesContractDetail", query: parameter });
    }
    // 查看项目结算详情
    else if (model === "viewSettlement") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_settlement/project/components/projectSettlementDetail",
        name: "projectSettlementDetail",
        query: parameter,
        meta: {
          title: `【查看】项目结算单`
        }
      });
      router.push({ name: "projectSettlementDetail", query: parameter });
    }
    // 查看项目订单详情
    else if (model === "viewProjectOrder") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_OrderManage/ERP_ProjectOrder/components/addProjectOrder",
        name: "addProjectOrder",
        query: parameter,
        meta: {
          title: `【查看】项目订单`
        }
      });
      // 路由跳转
      router.push({ name: "addProjectOrder", query: parameter });
    }
    // 查看验收清单详情
    else if (model === "viewAcceptanceForm") {
      useMultiTagsStoreHook().handleTags("push", {
        path: "/ERP_OrderManage/ERP_AcceptanceForm/components/selectAcceptanceForm",
        name: "selectAcceptanceForm",
        query: parameter,
        meta: {
          title: `【查看】验收清单`
        }
      });
      router.push({ name: "selectAcceptanceForm", query: parameter });
    }
  }

  // 用于页面刷新，重新获取浏览器地址栏参数并保存到标签页
  const initToDetail = (
    model:
      | "addPjctInvoice"
      | "viewPjctInvoice"
      | "editPjctInvoice"
      | "invoPjctInvoice"
      | "reInvoPjctInvoice"
      | "exaimePjctInvoice"
      | "viewSales"
      | "viewSettlement"
      | "viewProjectOrder"
      | "viewAcceptanceForm"
  ) => {
    if (getParameter) toDetail(getParameter, model);
  };

  return { toDetail, initToDetail, getParameter, router };
}
